package com.sinodata.bsm.cicp.collector.oracle;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class ListenerLog extends AbstractJDBCCollector {

    private final static Logger logger = Logger.getLogger(ListenerLog.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {

        List<ResPropertyValue> list = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResultSet rs = null;
        String[] propMetas = getPropMeta();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        String dbtype = this.getParamMap().get("DBType");
        String sqlString = this.getParamMap().get("collectCmd");
        //        System.out.println(sqlString);
        String dbname = this.getParamMap().get("DBName");
        try {
            rs = this.getCachedResultSet(dbtype, ip, dbname, sqlString);
            while (rs.next()) {
                propValues[0] = ResPropertyValue.createPropValue(rs.getString(1), propMetas[0]);
                propertyValueX.setValues(propValues);
                list.add(propertyValueX);
            }
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }
        return list;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "DBType", "collectCmd", "DBName" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "Oracle Archive Log Status" };
    }

    /*    private String getTmpFileName(String fileName, String ip) {
            String path = "tmp/";
            StringBuffer buffer = new StringBuffer();
            buffer.append(ip).append("_").append(fileName);
            return path + FileUtil.validateFileName(buffer.toString());
        }*/

    /**
     * 从文件的某个位置开始读，直至文件结束
     *
     * @param fileName
     * @param readStart
     * @param ip
     * @return
     */
    /*    private List readFile(String fullFileName, long readStart, String ip) {
            BufferedReader reader = null;
            String cmd = "filter -f " + fullFileName + " -s " + readStart;
            List<ResPropertyValueX> values = new ArrayList<ResPropertyValueX>();
            String[] propMetas = getPropMeta();
            try {
                //			reader = CollectorUtil.getReader(cmd, ip);
                reader = CmdServer.getReader(ip, cmd, (int) this.interval, 0);
                String line;
                while ((line = reader.readLine()) != null) {
                    ResPropertyValueX propertyValueX = new ResPropertyValueX();
                    ResPropertyValueX.PropValue[] propValues = new ResPropertyValueX.PropValue[propMetas.length];
                    Pattern patt = java.util.regex.Pattern.compile("\\d.*\\*.*\\*.*\\*.*\\*.*\\*.*");
                    Matcher match = patt.matcher(line);
                    if (match.find()) {
                        String[] respCloumn = line.trim().split("\\*");
                        String date = respCloumn[0].trim();
                        String values1 = dateChage(date);
                        String values2 = respCloumn[1].trim();
                        String values3 = respCloumn[2].trim();
                        String values4 = respCloumn[3].trim();
                        //                    String values5 = respCloumn[4].trim();
                        String values6 = respCloumn[5].trim();
                        String serverName = "";
                        String host = "";
                        String user1 = "";
                        //24-SEP-2009 17:44:21 * (CONNECT_DATA=(SID=orcl)(CID=(PROGRAM=)
                        //(HOST=__jdbc__)(USER=))) * (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.0.128)
                        //(PORT=34988)) * establish * orcl * 12505"
                        //patt=java.util.regex.Pattern.compile("\\S+CONNECT_DATA\\S+SID=.*");  // \\S+SID=.*\\S\\s\\S+CID=(PROGRAM=.*)\\s(host=.*)\\s(USER=.*)");
                        //\\S+CONNECT_DATA\\S+SID=.*\\S+CID=\\S+PROGRAM=\\S\\s\\S+HOST=.*\\S\\s\\S+USER=.*\\S
                        //values2= "(CONNECT_DATA=(SID=orcl)(CID=(PROGRAM=)(HOST=__jdbc__)(USER=sss)))";
                        patt = java.util.regex.Pattern.compile("\\S+CONNECT_DATA=\\S+SID=(\\w+||mark)\\S+CID=\\S+PROGRAM=\\S+HOST=(\\w+||mark)\\S+USER=(\\w+||mark)\\S");//\\S+\\s+\\S+CID=\\S+PROGRAM=\\S\\s\\S+HOST=(\\S+\\W\\S)\\S\\s\\S+USER=(\\w+).*");
                        match = patt.matcher(values2);
                        if (match.find()) {
                            if (match.group(1) == null || match.group(1).equals("")) {
                                serverName = "N/A";
                            } else {
                                serverName = match.group(1);
                            }
                            if (match.group(2) == null || match.group(2).equals("")) {
                                host = "N/A";
                            } else {
                                host = match.group(2);
                            }
                            if (match.group(3) == null || match.group(3).equals("")) {
                                user1 = "N/A";
                            } else {
                                user1 = match.group(3);
                            }
                        }
                        patt = java.util.regex.Pattern.compile("\\S+ADDRESS=\\S+PROTOCOL=tcp\\S+HOST=(\\d+\\S+\\d+||mark)\\S+PORT=");
                        match = patt.matcher(values3);
                        String hostIp = "";
                        if (match.find()) {
                            if (match.group(1) == null || match.group(1).equals("")) {
                                hostIp = "N/A";
                            } else {
                                hostIp = match.group(1);
                            }
                        }
                        propValues[0] = ResPropertyValueX.createPropValue(values1, propMetas[0]);
                        propValues[1] = ResPropertyValueX.createPropValue(values4, propMetas[1]);
                        propValues[2] = ResPropertyValueX.createPropValue(serverName, propMetas[2]);
                        propValues[3] = ResPropertyValueX.createPropValue(values6, propMetas[3]);
                        propValues[4] = ResPropertyValueX.createPropValue(values2, propMetas[4]);
                        propValues[5] = ResPropertyValueX.createPropValue(values3, propMetas[5]);
                        propValues[6] = ResPropertyValueX.createPropValue(host, propMetas[6]);
                        propValues[7] = ResPropertyValueX.createPropValue(user1, propMetas[7]);
                        propValues[8] = ResPropertyValueX.createPropValue(hostIp, propMetas[8]);
                        propertyValueX.setValues(propValues);
                        values.add(propertyValueX);
                    } else {
                        patt = java.util.regex.Pattern.compile("\\d.*\\*.*\\*.*\\*.*");
                        match = patt.matcher(line);
                        if (match.find()) {
                            String[] respCloumn1 = line.trim().split("\\*");
                            String v1 = respCloumn1[0].trim();
                            String v0 = dateChage(v1);
                            String v2 = respCloumn1[1].trim();
                            String v3 = respCloumn1[2].trim();
                            String v4 = respCloumn1[3].trim();
                            propValues[0] = ResPropertyValueX.createPropValue(v0, propMetas[0]);
                            propValues[1] = ResPropertyValueX.createPropValue(v2, propMetas[1]);
                            propValues[2] = ResPropertyValueX.createPropValue(v3, propMetas[2]);
                            propValues[3] = ResPropertyValueX.createPropValue(v4, propMetas[3]);
                            propValues[4] = ResPropertyValueX.createPropValue("N/A", propMetas[4]);
                            propValues[5] = ResPropertyValueX.createPropValue("N/A", propMetas[5]);
                            propValues[6] = ResPropertyValueX.createPropValue("N/A", propMetas[6]);
                            propValues[7] = ResPropertyValueX.createPropValue("N/A", propMetas[7]);
                            propValues[8] = ResPropertyValueX.createPropValue("N/A", propMetas[8]);
                            propertyValueX.setValues(propValues);
                            values.add(propertyValueX);
                        }

                    }

                }
                return values.size() == 0 ? null : values;
            } catch (IOException e) {
                logger.error("", e);
                return null;
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (Exception ex) {
                    }
                }
            }
        }*/

    /*    public String dateChage(String values1) {
            String[] resp = values1.trim().split("-");
            String resp1 = resp[0].trim();
            String resp2 = resp[1].trim();
            String resp3 = resp[2].trim();
            String[] month = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
            String[] month1 = { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" };
            for (int i = 0; i < 12; i++) {
                if (resp2.equals(month[i])) {
                    resp2 = month1[i];
                }
            }
            String values = resp1 + "-" + resp2 + "-" + resp3;
            return values;

        }*/

}
